angular.module('app.setting')
    .controller('settingCtrl', ['$scope', '$filter', '$modal', '$location', 'toaster', 'settingService', 'config',
        function ($scope, $filter, $modal, $location, toaster, settingService, config) {

            $scope.getSettings = function () {
                settingService.getSettings().then(function (res) {
                    $scope.settingInfo = res.data.data;
                }, function (res) {
                    toaster.pop("error", "错误", res.errMsg || "获取信息失败")
                });
            };
            $scope.getSettings();

            $scope.save = function () {
                if(!$scope.settingInfo.id){
                    toaster.pop("error", "错误",  "获取信息失败");
                    return;
                }
                settingService.setSettings($scope.settingInfo).then(function (res) {
                    $scope.settingInfo = res.data.data;
                }, function (res) {
                    toaster.pop("error", "错误", res.errMsg || "获取信息失败")
                });
            };


            $scope.clearNoNum = function (obj, attr) {
                //先把非数字的都替换掉，除了数字和.
                obj[attr] = obj[attr].replace(/[^\d.]/g, "");
                //必须保证第一个为数字而不是.
                obj[attr] = obj[attr].replace(/^\./g, "");
                //保证只有出现一个.而没有多个.
                obj[attr] = obj[attr].replace(/\.{2,}/g, "");
                //保证.只出现一次，而不能出现两次以上
                obj[attr] = obj[attr].replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
            }
        }]);
